package com.bookseek.xreader.views.adapter;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bookseek.xreader.R;
import com.bookseek.xreader.consts.XreaderConts;
import com.bookseek.xreader.utils.DxmItemClickListener;
import com.bookseek.xreader.utils.JkhlUtils;
import com.bookseek.xreader.vo.mock.TBookChapterVo;
import com.bookseek.xreader.vo.mock.TBookInfoVo;

import java.util.List;

public class BookChapterItemAdapter extends RecyclerView.Adapter<BookChapterItemAdapter.DataViewHolder> {
    private TBookInfoVo bookInfoVo;

    private DxmItemClickListener clickListener;

    public BookChapterItemAdapter(TBookInfoVo bookInfoVo,DxmItemClickListener clickListener) {
        this.bookInfoVo = bookInfoVo;
        this.clickListener = clickListener;
    }

    public void updateData(TBookInfoVo infoVo){
        this.bookInfoVo =infoVo;
        notifyDataSetChanged();
    }

    public int getChapterPos(Long chapterId){
        int i = 0;
        if(bookInfoVo != null && bookInfoVo.chapters != null){
            for(;i<bookInfoVo.chapters.size();i++){
                if(chapterId.equals(bookInfoVo.getChapters().get(i).id)){
                    break;
                }
            }
        }
        return i;
    }

    @NonNull
    @Override
    public DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.chapter_title_item, parent, false);

        return new DataViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DataViewHolder holder, int position) {
        List<TBookChapterVo> chapters = bookInfoVo.chapters;
        if(chapters != null && chapters.size() > 0){
            TBookChapterVo chapterVo = chapters.get(position);
            String btitle = JkhlUtils.toStr(chapterVo.btitle);
            if(chapterVo.id != null && chapterVo.id.equals(bookInfoVo.nowReadChapterId)){
                //holder.chapterTitleTv.setTextColor(ContextCompat.getColor(holder.chapterTitleTv.getContext(),R.color.layui_class_blue_color));
                SpannableString spanText = new SpannableString(btitle);
                ForegroundColorSpan color = new ForegroundColorSpan(ContextCompat.getColor(holder.chapterTitleTv.getContext(),R.color.layui_class_blue_color));
                spanText.setSpan(color,0,btitle.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                holder.chapterTitleTv.setText(spanText);
            }else{
                holder.chapterTitleTv.setText(btitle);
            }
            if(chapterVo.bnum != null){
                String bnumText = "["+chapterVo.bnum+"] 字";
                holder.chapterBcountTv.setText(bnumText);
            }
        }
    }

    @Override
    public int getItemCount() {
        List<TBookChapterVo> chapters = bookInfoVo.chapters;
        return chapters != null ? chapters.size() : 0;
    }

    class DataViewHolder extends  RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{

        private TextView chapterTitleTv;
        private TextView chapterBcountTv;
        public DataViewHolder(@NonNull View itemView) {
            super(itemView);
            chapterTitleTv = itemView.findViewById(R.id.chapterTitleTv);
            chapterBcountTv = itemView.findViewById(R.id.chapterBcountTv);
            // 为itemView设置点击和长按监听
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                // 获取当前position（注意：要用getAdapterPosition()而非getLayoutPosition()）
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) { // 防止position无效
                    clickListener.onItemClick(view, position);
                }
            }
        }

        @Override
        public boolean onLongClick(View view) {
            if (clickListener != null) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    clickListener.onItemLongClick(view, position);
                    return true; // 消费长按事件，避免触发点击事件
                }
            }

            return false;
        }
    }


}
